package com.ddwl.goods.controller.retail;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.annotation.Submit;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.goods.service.retail.ShopItemService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.ItemShelfBo;
import com.ddwl.schema.bo.goods.retail.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.retail.*;
import com.ddwl.schema.vo.shop.ShopVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @Author hemiaojun
 * @Date 2020/12/1 15:53
 */
@RestController
@RequestMapping("/shopItem")
@Api(tags = "店铺商品")
public class ShopItemController extends BaseController {

    @Resource
    private ShopItemService shopItemService;

    @PostMapping("/add")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    @ApiOperation("商户后台新增店铺商品")
    public RestVo add(@Valid @RequestBody ShopItemAddBo bo){
        bo.setShopId(getShopId());
        bo.setShopName(getShopName());
        shopItemService.createShopItem(bo);
        return RestVo.ok();
    }

    @PostMapping("/update")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    @ApiOperation("商户后台修改店铺商品")
    public RestVo update(@Valid @RequestBody ShopItemUpdateBo bo){
        bo.setShopId(getShopId());
        shopItemService.updateShopItem(bo);
        return RestVo.ok();
    }

    @PostMapping("/list")
    @Auth(required = false)
    @ApiOperation("小程序店铺按照分类获取商品列表")
    public RestVo<PageVo<ShopItemListVo>> list(@Valid @RequestBody PageBo<ShopItemListBo> pageBo){
        ShopItemListBo param = Optional.ofNullable(pageBo.getParam()).orElse(new ShopItemListBo());
        param.setUserId(getUserId());
        pageBo.setParam(param);
        return RestVo.ok(shopItemService.list(pageBo));
    }

    @PostMapping("/shelf")
    @ApiOperation("上下架商品")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo shelf(@Valid @RequestBody ItemShelfBo bo) {
        if (BaseEnum.ProductEnum.WBM.getName().equals(getProduct())) {
            ShopVo shopVo = getShopVo(bo.getShopId());
            bo.setShopId(shopVo.getId());
        }else{
            bo.setShopId(getShopId());
        }
        shopItemService.shelf(bo);
        return RestVo.ok();
    }

    @PostMapping("/batchShelf")
    @ApiOperation("批量上下架商品")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE})
    public RestVo batchShelf(@Valid @RequestBody ItemBatchShelfBo bo) {
        bo.setShopId(getShopId());
        shopItemService.batchShelf(bo);
        return RestVo.ok();
    }

    @PostMapping("/offShelf")
    @ApiOperation("运营后台下架店铺商品")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo offShelf(@Valid @RequestBody ItemOffShelfBo bo) {
        shopItemService.offShelf(bo,getUserId());
        return RestVo.ok();
    }

    @GetMapping("/get/{id}")
    @ApiOperation("商品详情")
    @Auth(required = false)
    public RestVo<ShopItemVo> get(@PathVariable("id") String id) {
        return RestVo.ok(shopItemService.get(id,getProduct()));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除商品")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo delete(@PathVariable("id") String id) {
        shopItemService.delete(id);
        return RestVo.ok();
    }

    @PostMapping("/page")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.CONSOLE})
    @ApiOperation("商户后台和运营后台店铺商品列表分页")
    public RestVo<PageVo<ShopItemVo>> page(@Valid @RequestBody PageBo<ShopItemPageBo> bo){
        if(BaseEnum.ProductEnum.SHOP_CONSOLE.getName().equals(getProduct())){
            bo.getParam().setShopId(getShopId());
        }
        return RestVo.ok(shopItemService.page(bo));
    }

    @PostMapping("/sort")
    @ApiOperation("商户后台店铺商品排序")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo sort(@RequestParam("itemId")String itemId,@RequestParam("itemSort")Integer itemSort){
        shopItemService.sort(itemId,itemSort);
        return RestVo.ok();
    }

    @GetMapping("/listSku/{itemId}")
    @Auth(required = false)
    @ApiOperation("获取商品sku信息")
    public RestVo<List<ActiveItemSkuVo>> listSku(@PathVariable("itemId") String itemId){
        return RestVo.ok(shopItemService.listSkuByItemId(itemId));
    }

    @PostMapping("/recommend")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE},required = false)
    @ApiOperation("购物车推荐商品")
    public RestVo<PageVo<MerchantShopItemPageVo>> recommend(@Valid @RequestBody PageBo<MerchantShopItemPageBo> bo){
        if (null == bo.getParam()) {
            bo.setParam(new MerchantShopItemPageBo());
        }
        bo.getParam().setRecommend(Boolean.TRUE);
        bo.getParam().setItemStatus(Integer.valueOf(GoodsEnum.ItemStatus.ONLINE.getCode()));
        return RestVo.ok(shopItemService.pageForMerchant(bo));
    }

    @PostMapping("/merchant/page")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序商家端 商品列表分页")
    public RestVo<PageVo<MerchantShopItemPageVo>> pageForMerchant(@Valid @RequestBody PageBo<MerchantShopItemPageBo> bo){
        if (null == bo.getParam()) {
            bo.setParam(new MerchantShopItemPageBo());
        }
        ShopVo shopVo = getShopVo(bo.getParam().getShopId());
        bo.getParam().setShopId(shopVo.getId());
        return RestVo.ok(shopItemService.pageForMerchant(bo));
    }

    @PostMapping("/merchant/add")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序商家端 新增店铺商品")
    @Submit
    public RestVo addForMerchant(@Valid @RequestBody MerchantShopItemAddBo bo){
        ShopVo shopVo = getShopVo(bo.getShopId());
        bo.setShopId(shopVo.getId());
        shopItemService.addForMerchant(bo);
        return RestVo.ok();
    }

    @PostMapping("/merchant/update")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("五八马小程序商家端 修改店铺商品")
    public RestVo updateForMerchant(@Valid @RequestBody MerchantShopItemUpdateBo bo){
        ShopVo shopVo = getShopVo(bo.getShopId());
        bo.setShopId(shopVo.getId());
        shopItemService.updateForMerchant(bo);
        return RestVo.ok();
    }

    @GetMapping("/listHot/{shopId}")
    @Auth(required = false,product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiModelProperty("热卖商品列表")
    public RestVo<List<ShopItemVo>> listHot(@PathVariable("shopId") String shopId){
        return RestVo.ok(shopItemService.listHot(shopId));
    }

    @PostMapping("/discount/selectPage")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("打折商品选择列表")
    public RestVo<PageVo<DiscountItemSelectPageVo>> discountItemSelectPage(@Valid @RequestBody PageBo<DiscountItemSelectPageBo> bo) {
        if (null == bo.getParam()) {
            bo.setParam(new DiscountItemSelectPageBo());
        }
        getShopVo(bo.getParam().getShopId());
        return RestVo.ok(shopItemService.discountItemSelectPage(bo));
    }

    @PostMapping("/discount/select")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("选择打折商品")
    public RestVo<DiscountItemSelectVo> discountItemSelect(@RequestParam String shopId, @RequestParam String itemId) {
        getShopVo(shopId);
        return RestVo.ok(shopItemService.discountItemSelect(itemId));
    }

    @PostMapping("/discount/batchAdd")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("添加打折商品")
    public RestVo addDiscountItem(@Valid @RequestBody DiscountItemBatchAddBo bo) {
        getShopVo(bo.getShopId());
        shopItemService.batchAddDiscountItem(bo);
        return RestVo.ok();
    }

    @PostMapping("/discount/add")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("添加打折商品")
    public RestVo addDiscountItem(@Valid @RequestBody DiscountItemAddBo bo) {
        getShopVo(bo.getShopId());
        shopItemService.addDiscountItem(bo);
        return RestVo.ok();
    }

    @PostMapping("/discount/update")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("修改打折商品")
    public RestVo updateDiscountItem(@Valid @RequestBody DiscountItemUpdateBo bo) {
        getShopVo(bo.getShopId());
        shopItemService.updateDiscountItem(bo);
        return RestVo.ok();
    }

    @PostMapping("/discount/page")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("打折商品分页查询")
    public RestVo<PageVo<DiscountItemPageVo>> pageDiscountItem(@Valid @RequestBody PageBo<DiscountItemPageBo> bo) {
        if (null == bo.getParam()) {
            bo.setParam(new DiscountItemPageBo());
        }
        getShopVo(bo.getParam().getShopId());
        return RestVo.ok(shopItemService.pageDiscountItem(bo));
    }

    @DeleteMapping("/discount/delete/{shopId}/{id}")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("打折商品删除")
    public RestVo deleteDiscountItem(@PathVariable String shopId,@PathVariable("id") String id) {
        getShopVo(shopId);
        shopItemService.deleteDiscountItem(id);
        return RestVo.ok();
    }

    @PostMapping("/agentItem/page")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("代理商商品分页")
    public RestVo<PageVo<AgentItemPageVo>> agentItemPage(@Valid @RequestBody PageBo<AgentItemPageBo> pageBo) {
        getShopVo(pageBo.getParam().getCurrentShopId());
        return RestVo.ok(shopItemService.agentItemPage(pageBo));
    }

    @PostMapping("/agentItem/copy")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("复制代理商商品")
    public RestVo copyAgentItem(@Valid @RequestBody AgentItemCopyBo bo) {
        getShopVo(bo.getCurrentShopId());
        shopItemService.copyAgentItem(bo);
        return RestVo.ok();
    }


}
